package com.mq.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mq.entity.User;
import com.mq.send.DelaySend;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author xwj
 * @date 2019/2/27
 */
@RestController
public class RabbitmqController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private DelaySend delaySend;
    @Autowired
    private ObjectMapper objectMapper;

    @GetMapping("/sendDirect/{exchange}/{routingKey}")
    public String sendDirect(String message, @PathVariable(value = "exchange") String exchange, @PathVariable(value = "routingKey") String routingKey) throws Exception {
        User user = new User(UUID.randomUUID().toString(), message, "123456", "sendDirect");
        rabbitTemplate.convertAndSend(exchange, routingKey, user);
        return "OK,sendDirect:" + message;
    }

    @GetMapping("/sendDirect")
    public String sendDirect(String message) throws Exception {
        User user = new User(UUID.randomUUID().toString(), message, "123456", "sendDirect");
        rabbitTemplate.convertAndSend("CalonDirectExchange", "CalonDirectRouting", user);
        return "OK,sendDirect:" + message;
    }

    @GetMapping("/sendDirectAck")
    public String sendDirect2(String message) throws Exception {
        User user = new User(UUID.randomUUID().toString(), message, "456789", "sendDirect2");
        rabbitTemplate.convertAndSend("CalonDirectExchange", "CalonDirectQueueAckRouting", objectMapper.writeValueAsString(user));
        return "OK,sendDirect2:" + message;
    }

    @GetMapping("/sendTopicFirst")
    public String sendTopicFirst(String message) {
        User user = new User(UUID.randomUUID().toString(), message, "123456", "sendTopicFirst");
        rabbitTemplate.convertAndSend("topicExchange", "topic.first", user);
        return "OK,sendTopicFirst:" + message;
    }

    @GetMapping("/sendTopicSecond")
    public String sendTopicSecond(String message) {
        User user = new User(UUID.randomUUID().toString(), message, "123456", "sendTopicSecond");
        rabbitTemplate.convertAndSend("topicExchange", "topic.second", user);
        return "OK,sendTopicSecond:" + message;
    }

    @GetMapping("/sendFanout")
    public String sendFanout(String message) {
        User user = new User(UUID.randomUUID().toString(), message, "123456", "sendFanout");
        rabbitTemplate.convertAndSend("fanoutExchange", null, user);
        return "OK,sendFanout:" + message;
    }

    @GetMapping("/sendDelay5s")
    public String sendDelay5s(String message) {
        User user = new User(UUID.randomUUID().toString(), message, "123456", "sendDelay5s");
        delaySend.sendDelay5s(user);
        return "OK,sendDelay5s:" + message;
    }

    @GetMapping("/sendDelay10s")
    public String sendDelay10s(String message) {
        User user = new User(UUID.randomUUID().toString(), message, "123456", "sendDelay10s");
        rabbitTemplate.convertAndSend("delay_direct", "10s", user);
        return "OK,sendDelay10s:" + message;
    }

    @GetMapping("/sendDelay30s")
    public String sendDelay30s(String message) {
        User user = new User(UUID.randomUUID().toString(), message, "123456", "sendDelay30s");
        rabbitTemplate.convertAndSend("delay_direct", "30s", user);
        return "OK,sendDelay30s:" + message;
    }

    @GetMapping("/sendDelay60s")
    public String sendDelay60s(String message) {
        User user = new User(UUID.randomUUID().toString(), message, "123456", "sendDelay60s");
        rabbitTemplate.convertAndSend("delay_direct", "60s", user);
        return "OK,sendDelay60s:" + message;
    }


}
